import {page, pageFunc} from "../common/PageBuilder";
import {BasePage, BasePageData} from "../common/core/BasePage";
import {pageMgr} from "../../modules/coreModule/managers/PageManager";
import {MessageInterface} from "../../modules/messageModule/MessageCommon";
import {messageListPage} from "../messageList/MessageListPage";
import {Message, MessageStatus, SystemSenderId} from "../../modules/messageModule/data/Message";
import {DataLoader, field} from "../../modules/coreModule/data/DataLoader";
import {QueryPage} from "../common/partPages/QueryPage";
import {blockLoading, showLoading} from "../../modules/coreModule/managers/LoadingManager";
import {CoreLoading} from "../../modules/coreModule/CoreCommon";
import {waitForLogin} from "../../modules/userModule/managers/UserManager";
import {UserPage} from "../common/partPages/UserPage";
import {ShareAppPage} from "../common/partPages/SharePage";

class Data extends BasePageData {

	@field([Message])
	public messages: Message[] = [];
	@field
	public unreadCnt: number = 0;
}

@page("message", "消息")
export class MessagePage extends BasePage<Data> {

	public data = new Data();

	public userPage: UserPage = new UserPage();
	public queryPage: QueryPage = new QueryPage(
		this.loadMessages.bind(this), {
			senderId: SystemSenderId,
			status: ["_.neq", MessageStatus.Deleted]
		});
	public shareAppPage = new ShareAppPage();

	// onLoad(e) {
	// 	super.onLoad(e);
	// }

	onShow() {
		super.onShow();
		this.queryPage.refresh();
	}

	@showLoading(CoreLoading.Mask)
	@blockLoading
	@waitForLogin
	private async loadMessages(queryParams) {
		const messages: Message[] = (await MessageInterface.GetMessages(queryParams))
			.messages.map(m => DataLoader.load(Message, m));
		// 按时间降序排列
		messages.sort((ma, mb) => mb.createdAt - ma.createdAt);
		const unreadCnt = messages.filter(m => m.status == MessageStatus.Unread).length;
		await this.setData({messages, unreadCnt});
	}

	@pageFunc
	toMessageList() {
		pageMgr().push(messageListPage).then();
	}

}




